<template>
  <div class="outer">
    <div class="user-info" v-if="token">
      <div class="user-name">
        <!-- <span>欢迎您 {{ userDetailData.username }}</span> -->
        <input
          type="file"
          name="file"
          value="上传头像"
          @change="handleToUpload"
          accept="image/png, image/jpeg, image/gif, image/jpg"
        />
        <img :src="userInfo.img" alt />
      </div>
      <div class="user-descri">
        <updateUserInfo :userDetailData="userDetailData"></updateUserInfo>
      </div>
    </div>
    <div class="isLogin" v-else>
      <img src="./Info/login.png" alt="" />
      <h1>对不起，您还未登录！</h1>
    </div>
  </div>
</template>

<script>
import updateUserInfo from "./Info/updateUserDetail.vue"
import { mapState, mapActions } from "vuex"
import api from "@/api"

export default {
  name: "userInfo",
  data() {
    return {
      name: "Monday",
      userDetailData: {}
    }
  },
  components: {
    updateUserInfo
  },
  computed: {
    ...mapState(["userInfo", "token", "userImg"])
  },
  mounted() {
    this.$api.getUserInfo().then((res) => {
      this.userDetailData = res.data.user
    })
  },
  methods: {
    ...mapActions(["updateUserInfo"]),
    handleToUpload(ev) {
      let file = ev.target.files[0]
      api.uploadFiles(file).then((res) => {
        // console.log(res);
        let status = res.data.status
        if (status === 200) {
          this.updateUserInfo(res.data.user)
          this.$notify({
            title: "提示",
            message: res.data.message,
            type: "success",
            position: "top-right",
            duration: 1000
          })
        } else {
          this.$notify({
            title: "提示",
            message: res.data.message,
            type: "error",
            position: "top-right",
            duration: 1000
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.outer {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background: rgba(240, 240, 240, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  .user-info {
    background: #ffffff;
    box-shadow: 0 20px 40px 0 rgb(174 186 205 / 11%);
    border-radius: 5px;
    padding: 20px 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    .user-name {
      padding: 40px 0;
      display: flex;
      align-items: center;
      input {
        width: 80px;
        height: 80px;
        /* margin:-20rem auto 0; */
        border: 1px dashed #ababab;
        border-radius: 50%;
        opacity: 0;
        cursor: pointer;
        position: absolute;
        // margin-top: -10rem;
      }
      img {
        width: 80px;
        height: 80px;
        border-radius: 50%;
      }
      span {
        margin-left: 10px;
      }
    }
    .user-descri {
      display: flex;
      justify-content: center;
      padding: 20px 0;
    }
  }
  .isLogin {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: #ffffff;
    img {
      width: 250px;
      margin-bottom: 30px;
    }
  }
}
</style>
